package cc.angis.xiugallery;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import cc.angis.xiugallery.common.GlobalConst;
import cc.angis.xiugallery.image.ui.ImageGridFragment;
import cc.angis.xiugallery.image.ui.JBImageGridFragment;

@SuppressLint("NewApi") 
public class SampleListFragment extends  android.support.v4.app.ListFragment {

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.list, null);
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		SampleAdapter adapter = new SampleAdapter(getActivity());
//		for (int i = 0; i < 20; i++) {
//			adapter.add(new SampleItem(new  android.support.v4.app.Fragment(), "Sample List", android.R.drawable.btn_star));
//		}
		adapter.add(new SampleItem(new  android.support.v4.app.Fragment(), this.getString(R.string.menu_galary), R.drawable.ic_launcher));
		adapter.add(new SampleItem(new  android.support.v4.app.Fragment(), this.getString(R.string.menu_favourite), R.drawable.ic_launcher));
		adapter.add(new SampleItem(new  android.support.v4.app.Fragment(), this.getString(R.string.menu_comment), R.drawable.ic_launcher));
		adapter.add(new SampleItem(new  android.support.v4.app.Fragment(), this.getString(R.string.menu_settings), R.drawable.ic_launcher));
		
		setListAdapter(adapter);
	}

	private class SampleItem {
		public  android.support.v4.app.Fragment frag;
		public String tag;
		public int iconRes;
		public SampleItem( android.support.v4.app.Fragment fram, String tag, int iconRes) {
			this.frag = frag; 
			this.tag = tag; 
			this.iconRes = iconRes;
		}
	}

	public class SampleAdapter extends ArrayAdapter<SampleItem> {

		public SampleAdapter(Context context) {
			super(context, 0);
		}

		public View getView(final int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, null);
			}
			ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
			TextView title = (TextView) convertView.findViewById(R.id.row_title);
			icon.setImageResource(getItem(position).iconRes);
			title.setText(getItem(position).tag);
//			convertView.setOnClickListener(new OnClickListener() {
//				
//				@Override
//				public void onClick(View v) {
//					// TODO Auto-generated method stub
//					((MainActivity)v.getContext()).showAbove();
//				}
//			});
			return convertView;
		}

	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		int curIndex = v.getContext().getSharedPreferences(GlobalConst.PREF_NAME, 0).getInt(GlobalConst.PREF_CUR_INDEX, -1);
		if (position != curIndex) {
			android.support.v4.app.FragmentTransaction t = ((MainActivity)v.getContext()).getSupportFragmentManager().beginTransaction();
			t.remove(((MainActivity)v.getContext()).getSupportFragmentManager().findFragmentById(R.id.content));
			switch (position) {
				case 0:
//					t.add(R.id.content, new GroupingGridFragment(), GlobalConst.TAG_FRAGMENT_GALLERY);
					t.add(R.id.content, JBImageGridFragment.newInstance(GlobalConst.CAMERA_IMAGE_BUCKET_ID, GlobalConst.INTENT_GROUP_TYPE_GALLERY, GlobalConst.MenuType.MENU_TYPE_IMAGEDISPLAY, ""));
					break;
				case 1:
					t.add(R.id.content, new FavouriteGridFragment(), GlobalConst.TAG_FRAGMENT_FAVOURITE);
					break;
				case 2:
					t.add(R.id.content, new PostListFragment(), GlobalConst.TAG_FRAGMENT_COMMENT);
					break;
				case 3:
					t.add(R.id.content, new SettingsFragment(), GlobalConst.TAG_FRAGMENT_SETTINGS);
					break;
				default:
					break;
			}
			t.commit();
		}
		((MainActivity)v.getContext()).showAbove();
		v.getContext().getSharedPreferences(GlobalConst.PREF_NAME, 0).edit().putInt(GlobalConst.PREF_CUR_INDEX, position).commit();
		super.onListItemClick(l, v, position, id);
	}

}
